{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bea33bbe",
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'ZhipuAiClient' from 'zai' (d:\\workspace\\mysite-appStaticWeb\\.venv\\Lib\\site-packages\\zai\\__init__.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mImportError\u001b[39m                               Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mzai\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ZhipuAiClient\n\u001b[32m      2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mstock_analyzer\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m StockAnalyzer\n",
      "\u001b[31mImportError\u001b[39m: cannot import name 'ZhipuAiClient' from 'zai' (d:\\workspace\\mysite-appStaticWeb\\.venv\\Lib\\site-packages\\zai\\__init__.py)"
     ]
    }
   ],
   "source": [
    "from zai import ZhipuAiClient\n",
    "from ..core.analyzer import StockAnalyzer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a68fa098",
   "metadata": {},
   "outputs": [],
   "source": [
    "def analyze_stock(stock_code):\n",
    "    analyzer = StockAnalyzer()\n",
    "    report = analyzer.analyze_stock(stock_code)\n",
    "    prompt = f\"\"\"\n",
    "我打算以最新价格买入，在三个交易日内卖出。分析以下股票是否能够买入，返回结果可以是1:立即买入、-1:风险高于收益，不适合买入、0:当前不是买点需要观望这三种，请不要返回其他内容,仅返回对应的数字。    \n",
    "    {report}\n",
    "    \"\"\"\n",
    "    client = ZhipuAiClient(api_key=\"554b0440621a402ba40c9ee3de113f29.wGxg4Y9PHYJ0D8BE\")  # 请填写您自己的 API Key\n",
    "    response = client.chat.completions.create(\n",
    "        model =\"glm-4.5-flash\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": \"你是一个专业的中国股票分析师。\"},\n",
    "            {\"role\": \"user\", \"content\": prompt}\n",
    "        ],\n",
    "        thinking={\n",
    "            \"type\": \"disabled\",    # 启用深度思考模式\n",
    "        },\n",
    "        max_tokens=4096,          # 最大输出 tokens\n",
    "        temperature=0.6           # 控制输出的随机性\n",
    "    )\n",
    "    return response.choices[0].message.content\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c826cc25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n"
     ]
    }
   ],
   "source": [
    "# 测试函数\n",
    "stock_code = '603087'\n",
    "result = analyze_stock(stock_code)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6fecb9cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_sock_list import get_hs300_main_board_stocks\n",
    "stock_list = get_hs300_main_board_stocks()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "90dccc47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['601136', '601059', '002709', '002050', '002074', '603019', '002460', '600089', '601689', '002600', '603986', '000977', '002241', '000938', '000983', '600111', '000157', '000063', '600584', '600588', '603799', '600362', '601699', '605117', '603296', '002920']\n"
     ]
    }
   ],
   "source": [
    "# 获取换手率大于2的股票代码\n",
    "codes = stock_list[stock_list['换手率(%)'] > 3]['股票代码'].tolist()\n",
    "print(codes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bbfd35b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在分析股票：601136\n",
      "601136 分析结果：-1\n",
      "正在分析股票：601059\n",
      "601059 分析结果：-1\n",
      "正在分析股票：002709\n",
      "002709 分析结果：-1\n",
      "正在分析股票：002050\n",
      "002050 分析结果：-1\n",
      "正在分析股票：002074\n",
      "002074 分析结果：-1\n",
      "正在分析股票：603019\n",
      "603019 分析结果：-1\n",
      "正在分析股票：002460\n",
      "002460 分析结果：-1\n",
      "正在分析股票：600089\n",
      "600089 分析结果：-1\n",
      "正在分析股票：601689\n",
      "601689 分析结果：-1\n",
      "正在分析股票：002600\n",
      "002600 分析结果：-1\n",
      "正在分析股票：603986\n",
      "603986 分析结果：-1\n",
      "正在分析股票：000977\n",
      "000977 分析结果：-1\n",
      "正在分析股票：002241\n",
      "002241 分析结果：-1\n",
      "正在分析股票：000938\n",
      "000938 分析结果：-1\n",
      "正在分析股票：000983\n",
      "000983 分析结果：-1\n",
      "正在分析股票：600111\n",
      "600111 分析结果：-1\n",
      "正在分析股票：000157\n",
      "000157 分析结果：-1\n",
      "正在分析股票：000063\n",
      "000063 分析结果：-1\n",
      "正在分析股票：600584\n",
      "600584 分析结果：-1\n",
      "正在分析股票：600588\n",
      "600588 分析结果：0\n",
      "正在分析股票：603799\n",
      "603799 分析结果：-1\n",
      "正在分析股票：600362\n",
      "600362 分析结果：-1\n",
      "正在分析股票：601699\n",
      "601699 分析结果：-1\n",
      "正在分析股票：605117\n",
      "605117 分析结果：-1\n",
      "正在分析股票：603296\n",
      "603296 分析结果：-1\n",
      "正在分析股票：002920\n",
      "002920 分析结果：-1\n",
      "所有股票分析完成。\n",
      "结果汇总：\n",
      "601136: -1\n",
      "601059: -1\n",
      "002709: -1\n",
      "002050: -1\n",
      "002074: -1\n",
      "603019: -1\n",
      "002460: -1\n",
      "600089: -1\n",
      "601689: -1\n",
      "002600: -1\n",
      "603986: -1\n",
      "000977: -1\n",
      "002241: -1\n",
      "000938: -1\n",
      "000983: -1\n",
      "600111: -1\n",
      "000157: -1\n",
      "000063: -1\n",
      "600584: -1\n",
      "600588: 0\n",
      "603799: -1\n",
      "600362: -1\n",
      "601699: -1\n",
      "605117: -1\n",
      "603296: -1\n",
      "002920: -1\n"
     ]
    }
   ],
   "source": [
    "# 股票代码Python列表（字符串类型，保留前导0）\n",
    "stock_codes = codes\n",
    "\n",
    "import time\n",
    "\n",
    "results = {}\n",
    "\n",
    "for code in stock_codes:\n",
    "    print(f\"正在分析股票：{code}\")\n",
    "    try:\n",
    "        result = analyze_stock(code)\n",
    "        print(f\"{code} 分析结果：{result}\")\n",
    "        results[code] = result\n",
    "    except Exception as e:\n",
    "        print(f\"{code} 分析出错：{e}\")\n",
    "        results[code] = f\"分析出错：{e}\"\n",
    "    # 每2分钟分析一次\n",
    "    time.sleep(120)\n",
    "\n",
    "print(\"所有股票分析完成。\")\n",
    "print(\"结果汇总：\")\n",
    "for code, res in results.items():\n",
    "    print(f\"{code}: {res}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d997ca51",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8bd69376",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'601136': '-1',\n",
       " '601059': '-1',\n",
       " '002709': '-1',\n",
       " '002050': '-1',\n",
       " '002074': '-1',\n",
       " '603019': '-1',\n",
       " '002460': '-1',\n",
       " '600089': '-1',\n",
       " '601689': '-1',\n",
       " '002600': '-1',\n",
       " '603986': '-1',\n",
       " '000977': '-1',\n",
       " '002241': '-1',\n",
       " '000938': '-1',\n",
       " '000983': '-1',\n",
       " '600111': '-1',\n",
       " '000157': '-1',\n",
       " '000063': '-1',\n",
       " '600584': '-1',\n",
       " '600588': '0',\n",
       " '603799': '-1',\n",
       " '600362': '-1',\n",
       " '601699': '-1',\n",
       " '605117': '-1',\n",
       " '603296': '-1',\n",
       " '002920': '-1'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9abbf43b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for code, res in results.items():\n",
    "    if res == '1':\n",
    "        print(f\"{code} 分析结果：{res}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0486c26",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
